﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


/// <summary>
/// Www加载 AssetBundle
/// 问题: 先执行LoadAssetBundle才能继续执行WWW加载
/// </summary>
namespace ABFW
{

    public class SingleABLoader : System.IDisposable
    {

        //引用类,资源加载类
        private AssetLoader _AssetLoader;
        //委托
        private DelLoadComplete _LoadCompleteHandler;
        //AssetBundle 名称
        private string _ABName;
        //AssetBundle 下载路径
        private string _ABDownLoadPath;

        public SingleABLoader(string abName,DelLoadComplete loadComplete)
        {
            _AssetLoader = null;
            _ABName = abName;
            //委托定义
            _LoadCompleteHandler = loadComplete;
            //AB包下载路径
            _ABDownLoadPath = PathTools.GetWWWPath() + "/" + _ABName;
        }

        //通过WWW加载AssetBundle 资源包
        public IEnumerator LoadAssetBundle()
        {
            using (WWW www = new WWW(_ABDownLoadPath))
            {
                yield return www;
                //WWW下载AB包完成
                if (www.progress >= 1)
                {
                    //获取AssetBundle实例
                    AssetBundle abObj = www.assetBundle;
                    if(abObj != null)
                    {
                        //实例化引用类
                        _AssetLoader = new AssetLoader(abObj);
                        //AssetBundle下载完毕,调用委托
                        if (_LoadCompleteHandler!= null)
                        {
                            _LoadCompleteHandler.Invoke(_ABName);
                        }
                    }else
                    {
                        Debug.Log(GetType() + "/LoadAssetBundle()/WWW 下载出错,请检查! AssetBundle URL: " + _ABDownLoadPath + "错误信息： " + www.error);
                    }
                }
            }//usingEnd
        }

        /// <summary>
        /// 加载(AB包内资源)
        /// </summary>
        /// <param name="assetName"></param>
        /// <param name="isCache"></param>
        /// <returns></returns>
        public UnityEngine.Object LoadAsset(string assetName,bool isCache)
        {
            if (_AssetLoader!=null)
            {
                return _AssetLoader.LoadAsset(assetName, isCache);
            }
            Debug.LogError(GetType() + "/LoadAsset()/参数_AssetLoader==null");
            
            return null;
        }


        /// <summary>
        /// 卸载AB包中的资源
        /// </summary>
        /// <param name="asset"></param>
        public void UnLoadAsset(UnityEngine.Object asset)
        {
            if (_AssetLoader != null)
            {
                _AssetLoader.UnLoadAsset(asset);
            }else
            {
                Debug.LogError(GetType() + "/UnLoadAsset()/参数 _AssetLoader = Null");
            }
        }
        /// <summary>
        /// 释放资源
        /// </summary>
        public void Dispose()
        {
            if(_AssetLoader != null)
            {
                _AssetLoader.Dispose();
                _AssetLoader = null;
            }
            else
            {
                Debug.LogError(GetType() + "/Dispose()/参数 _AssetLoader = Null,请检查!");
            }
        }

        /// <summary>
        /// 释放当前AssetBundle资源包,且卸载所有资源
        /// </summary>
        public void DisposeAll()
        {
            if(_AssetLoader != null)
            {
                _AssetLoader.DisposeAll();
                _AssetLoader = null;
            }else
            {
                Debug.LogError(GetType() + "SingleABLoader/DisposeAll/_AssetLoader为空");
            }
        }

        /// <summary>
        /// 查询当前AssetBundle包中所有的资源
        /// </summary>
        /// <returns></returns>
        public string[] RetrivalAllAssetName()
        {
            if(_AssetLoader != null)
            {
                return _AssetLoader.RetriveAllAssetName();
            }
            Debug.LogError(GetType() + "SingleABLoader/RetrivalAllAssetName/AssetLoader为空");
            return null;
        }


    }
}
